package cn.tedu.design;
/**本类用于测试单例设计模式方案2--懒汉式--面试重点!*/
public class TestSingle2 {
    public static void main(String[] args) {
        MySingle2 s1 = MySingle2.getSingle2();
        MySingle2 s2 = MySingle2.getSingle2();
        MySingle2 s3 = MySingle2.getSingle2();
//        MySingle2 s1 = MySingle2.single2;
//        MySingle2 s2 = MySingle2.single2;
//        MySingle2 s3 = MySingle2.single2;
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);


    }
}
class MySingle2{
    private MySingle2(){}
    static private       MySingle2 single2;
    static public  MySingle2 getSingle2(){
        //判断引用类型变量的值是否为null
        if (single2==null) {//如果为null,说明之前没创建过对象
            single2=new MySingle2();//创建对象
        }
        return single2;//直接返回之前创建的对象即可
    }

}